package prepare_data

import (
	"anubis-framework/pkg/io"
	"gorm.io/gorm"
)

// 准备数据：收盘价最少的(10*账号数量+4)只股票
type MinClosePricePrepareData struct {
	db *gorm.DB
}

// 初始化
func (m *MinClosePricePrepareData) Init(db *gorm.DB) {
	m.db = db
}

// 准备数据：清空quant_stock_filter表，从stock_transaction_data_all表中向quant_stock_filter表中插入记录，按照close_price升序排列，包括正在持有的股票
func (m *MinClosePricePrepareData) DoPrepareData(currentDate string) {
	io.Infoln("准备数据：清空quant_stock_filter表，从stock_transaction_data_all表中向quant_stock_filter表中插入记录，按照close_price升序排列，包括正在持有的股票")

	// 清空quant_stock_filter表
	m.db.Exec("truncate table quant_stock_filter")

	// 从stock_transaction_data_all表中向quant_stock_filter表中插入记录，按照close_price升序排列，包括正在持有的股票
	m.db.Exec("insert /*+ append */ into quant_stock_filter(stock_code, close_price, total_market_value) "+
		"select distinct stda.code_, stda.close_price, stda.total_market_value "+
		"from stock_transaction_data_all stda "+
		"where stda.date_=to_date(?, 'yyyy-mm-dd') order by stda.close_price asc", currentDate)
	//c.db.Exec("insert into quant_stock_filter(stock_code, total_market_value) "+
	//	"select distinct stda.code_, stda.total_market_value "+
	//	"from stock_transaction_data_all stda "+
	//	"where stda.date_=to_date(?, 'yyyy-mm-dd') and stda.code_ not in("+
	//	"select t.stock_code from quant_stock_transact_record t "+
	//	"where t.buy_date is not null and t.buy_price is not null and t.buy_amount is not null "+
	//	"and t.sell_date is null and t.sell_price is null and t.sell_amount is null)", currentDate)
}
